///
//	AssetManagerErrorExample v 1.7.1
//  Russell Lowke, December 18th 2011
//
//	Copyright (c) 2009-2011 Russell Lowke
//	see http://www.lowkemedia.com for more information
//
//	Permission is hereby granted, free of charge, to any person obtaining a 
//	copy of this software and associated documentation files (the "Software"), 
//	to deal in the Software without restriction, including without limitation 
//	the rights to use, copy, modify, merge, publish, distribute, sublicense, 
//	and/or sell copies of the Software, and to permit persons to whom the 
//	Software is furnished to do so, subject to the following conditions:
// 
//	The above copyright notice and this permission notice shall be included in 
//	all copies or substantial portions of the Software.
// 
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
//	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
//	DEALINGS IN THE SOFTWARE. 
//
//
//	Note: You might need to set permissions for local file access from the 
//	Flash Global Security Settings panel, see,
//		http://www.macromedia.com/support/documentation/en/flashplayer/help/
//		and select "Global Security Settings Panel"
//
//  Note: The compiler setting for this project has been set to -use-network=false
//  This allows loading of local files but not loading over the web.

//
// This example expands upon the regular AssetManagerExample, showing you how 
//  to catch logging errors and display them in an Alert. 
// Note especially the LogHandler.handleLogs() method.
//
//	In this case, when loading the assets/ship.swf asset, the name of the file
//	has intentionally been misspelled with an extra 'p' on the end (shipp.swf).
//	The file won't load and you see a warning alert appear.
//
// The Lowke Media alert utility at com.lowke.utils.as3.Alert is used to show  
//  the logging error.


package 
{	
	import com.lowke.alert.Alert;
	import com.lowke.assetLoader.AssetLoader;
	import com.lowke.assetLoader.event.LoaderProgressEvent;
	import com.lowke.assetManager.AssetManager;
	import com.lowke.logAs.LogAs;
	import com.lowke.logAs.event.LogAsEvent;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	
	[SWF(width="640", height="480", backgroundColor="#000000", frameRate="30")]
	public class LoggedErrorAlertExample extends Sprite 
	{	
		private var _assetManager:AssetManager;			// instance of AssetManager
		
		public function LoggedErrorAlertExample() 
		{	
			// tell the Alerts to draw on the stage
			Alert.defaultParent = stage;
			
			// get a refernece to the AssetManager
			_assetManager = new AssetManager();
			
			// let's listen to the load progress.
			//  We need to listen to the AssetLoader for load progress
			//   as the AssetManager uses the AssetLoader to load files.
			AssetLoader.addEventListener(LoaderProgressEvent.PROGRESS, printProgress);
			
			// let's also listen to the logger for any logging events
			LogAs.addEventListener(LogAsEvent.LOG_AS_EVENT, LogHandler.handleLog);
			
			// batch cache some files
			_assetManager.cacheFile("config.xml");							// Note: files are retrieved by their url
			_assetManager.cacheFile("assets/Blue-Planet-Earth.jpg");
			_assetManager.cacheClass("assets/text.swf", "PlanetEarth");		// Note: classes are retrieved by their class name
			_assetManager.cacheClass("assets/shipp.swf", "Ship");			// <<--- ship.swf asset intentionally spelt wrong so we see an error
			_assetManager.cacheFile("assets/whistle.mp3", false);
			_assetManager.cacheClassDefinition("assets/ufo.swf", "UFO");
			
			// when batch finished loading call ready()
			_assetManager.whenDone(ready);
		}
		
		private function printProgress(event:LoaderProgressEvent):void 
		{
			trace("Percent loaded:" + Math.round(event.percentDone*100) + "%");	
		}
		
		private function ready(event:Event = null):void 
		{	
			// rememeber to remove event listeners
			AssetLoader.removeEventListener(LoaderProgressEvent.PROGRESS, printProgress);
			
			//
			// your assets are loaded and ready
			
			// print the XML data
			trace("config XML:" + _assetManager.retrieve("config.xml"));
			
			// display the background
			addChild(_assetManager.retrieve("assets/Blue-Planet-Earth.jpg"));
			
			// center and show "Planet Earth" sprite
			var sprite:Sprite = _assetManager.retrieve("PlanetEarth");
			sprite.x = (stage.width - sprite.width)/2;	
			sprite.y = 26;
			addChild(sprite);
			
			// show "Ship" MovieClip
			var ship:MovieClip = _assetManager.retrieve("Ship");
			if (ship) {
				ship.x = 100; ship.y = 100;
				addChild(ship);
			}
			
			// add four to eight UFOs
			var ufoClass:Class = _assetManager.retrieve("UFO");
			var nUFOs:uint = randomInt(4, 8);				
			for (var i:uint = 0; i < nUFOs; ++i) {
				
				// create an asteroid MovieClip from Class definition
				var ufo:MovieClip = new ufoClass();
				
				// randomly place UFO
				ufo.x = randomInt(ufo.width, stage.width - ufo.width);
				ufo.y = randomInt(100, stage.height - ufo.height);
				addChild(ufo);
			}
			
			// play whistle sound
			_assetManager.retrieve("assets/whistle.mp3").play();
			
			// clear all assets
			_assetManager.clear();
		}
		
		//
		// generate a random integer from low value to high value
		public static function randomInt(low:int, high:int):int
		{
			var range:int = (high + 1) - low;
			var rtn:int = Math.floor(Math.random() * range) + low;
			
			return rtn;
		}
	}
}